package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Document;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class DocumentDao {
	DBHelp<Document> dbhelp = new DBHelp<Document>();
	public List<Document> findAll(String id) {
		
		String sql = "select * from t_document where projectid = ? order by createtime desc";
		return dbhelp.executeQueryForList(sql,new DocumentRowMapper(),id);
	}
	
	private class DocumentRowMapper implements RowMapper<Document>{

		public Document mapperRow(ResultSet rs) throws SQLException {
			Document doc = new Document();
			doc.setId(rs.getString("id"));
			doc.setTitle(rs.getString("title"));
			doc.setContent(rs.getString("content"));
			doc.setCreatetime(rs.getString("createtime"));
			doc.setEmployeeid(rs.getString("employeeid"));
			doc.setProjectid(rs.getString("projectid"));
			
			return doc;
		}
	}

	public void save(Document doc) {
		String sql = "insert into t_document(id,title,content,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		dbhelp.executeSQL(sql, doc.getId(),doc.getTitle(),doc.getContent(),doc.getCreatetime(),doc.getEmployeeid(),doc.getProjectid());
	}

	public void del(String id) {
		String sql = "delete from t_document where id = ?";
		dbhelp.executeSQL(sql, id);
	}

	public void update(String id, String title, String content) {
		String sql = "update t_document set title = ?,content = ? where id = ?";
		dbhelp.executeSQL(sql, title,content,id);
	}

	public Document findById(String id) {
		String sql = "select * from t_document where id = ?";
		return dbhelp.executeQueryForObject(sql, new DocumentRowMapper(), id);
	}

	
}
